package com.fahai.web.service.impl;

import com.fahai.common.PageVo;
import com.fahai.web.dao.MenuDao;
import com.fahai.web.dao.RoleMenuDao;
import com.fahai.web.dao.UserRoleDao;
import com.fahai.web.entity.Menu;
import com.fahai.web.entity.RoleMenu;
import com.fahai.web.entity.UserRole;
import com.fahai.web.service.RoleMenuService;
import com.fahai.web.vo.RoleMenuVo;
import com.fahai.web.vo.SysGroupVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by sky on 17/5/11.
 * 修改权限组业务层
 */
@Service
@Transactional(rollbackFor = DataAccessException.class)
public class RoleMenuServiceImpl implements RoleMenuService {


    private static Logger logger = LoggerFactory.getLogger(RoleMenuServiceImpl.class);

    @Autowired
    RoleMenuDao roleMenuDao;

    @Autowired
    MenuDao menuDao;

    @Autowired
    UserRoleDao userRoleDao;

    /**
     * 查看所有的权限组
     *
     * @return
     */
    @Override
    public List<RoleMenuVo> findRoleMenuList() {
        List<RoleMenuVo> roleMenuVoList = roleMenuDao.selectMenuRoleList();
        return roleMenuVoList;
    }


    /**
     * 查看所有的菜单列表
     * @return
     */
    @Override
    public List<Menu> findAllMenu(){
        List<Menu> menuList = menuDao.selectAll();
        return menuList;
    }


    /**
     * 数据聚合处理,返回前端所需要的数据
     */
    @Override
    public List<SysGroupVo> findMenuList() {
        List<RoleMenuVo> roleMenuList = findRoleMenuList();
        List<SysGroupVo> sysGroupVoList = new ArrayList<>();
        List<UserRole> userRoleList = userRoleDao.selectAll();
        //初始化权限组数据存儲容器vo
        for (UserRole ur : userRoleList) {
            SysGroupVo sysGroupVo = new SysGroupVo();
            sysGroupVo.setRoleId(ur.getRoleId());
            sysGroupVo.setGroupName(ur.getRoleName());
            sysGroupVo.setGroupRole(ur.getRemark());
            //默认给所有只读权限
            sysGroupVo.setCustomerManager(1);
            sysGroupVo.setLawyerManager(1);
            sysGroupVo.setConsultManager(1);
            sysGroupVo.setChannelManager(1);
            sysGroupVo.setFinanceManager(1);
            sysGroupVo.setOperateManager(1);
            sysGroupVo.setMarketManager(1);
            sysGroupVo.setScenceManager(1);
            sysGroupVo.setDataManager(1);
            sysGroupVo.setSysuserManager(1);

            sysGroupVoList.add(sysGroupVo);
        }
        for (RoleMenuVo rmv : roleMenuList) {
            System.out.println(rmv.toString());
        }

        //遍历所有权限列表的集合,菜单列表更改或增加新功能则这里也要相应修改，this可以改为enum
        for (RoleMenuVo rmv : roleMenuList) {    //角色菜单所有权限数据
            for (SysGroupVo sgv : sysGroupVoList) {     //前端数据展示list-vo
                if (sgv.getRoleId() == rmv.getRoleId()) {
                    if (rmv.getMenuName().equals("客户管理")) {
                        sgv.setCustomerManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("律师管理")) {
                        sgv.setLawyerManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("咨询管理")) {
                        sgv.setConsultManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("渠道管理")) {
                        sgv.setChannelManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("财务结算管理")) {
                        sgv.setFinanceManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("运营规则设定")) {
                        sgv.setOperateManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("市场活动设定")) {
                        sgv.setMarketManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("场景充值管理")) {
                        sgv.setScenceManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("数据分析")) {
                        sgv.setDataManager(rmv.getRole());
                    } else if (rmv.getMenuName().equals("账号管理")) {
                        sgv.setSysuserManager(rmv.getRole());
                    }
//                    ...
                }
            }
        }
        return sysGroupVoList;
    }

    /**
     * 修改权限组
     */
    @Override
    public void updateSysMenu(SysGroupVo sgv) {
//        List<RoleMenu> roleMenuList = new ArrayList<>();
        List<Menu> menuList = menuDao.selectAll();
        for (Menu m : menuList) {
            RoleMenu roleMenu = setRoleMenuValue(m, sgv);
//            roleMenuList.add(roleMenu);
            logger.debug(roleMenu.toString());
            roleMenuDao.updateByDoubleId(roleMenu);    //根据roleid和menuid联合主键共同更新
        }
    }

    /**
     * 添加权限组
     *
     * @param sgv
     */
    @Override
    public void saveSysMenu(SysGroupVo sgv) {
        List<Menu> menuList = menuDao.selectAll();
        for (Menu m : menuList) {
            RoleMenu roleMenu = setRoleMenuValue(m, sgv);
//            roleMenuList.add(roleMenu);
            logger.debug(roleMenu.toString());
//            roleMenuDao.insert(roleMenu);    //根据roleid和menuid联合主键共同更新
            roleMenuDao.insertByDoubleId(roleMenu);
        }
    }

    @Override
    public PageVo selectRoleMenuList(PageVo pageVo) {

        Page<Map<String,Object>> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<UserRole> getData=userRoleDao.selectAll();

        pageVo.setTotal(new Long(userRoleDao.selectCount(new UserRole())));

        List<Integer> roidArray=new ArrayList<Integer>();

        for (UserRole ur:getData) {

            roidArray.add(ur.getRoleId());

        }

        List<Map<String,Object>>  roleData=roleMenuDao.selectRoleMenu(roidArray);


        List<Menu> menuData=menuDao.selectAll();



        List< Map<String,Object>> roleMapData=new ArrayList<Map<String,Object>>();


        for (UserRole ur:getData) {


            Map<String,Object>  pushMap=new LinkedHashMap<String,Object>();

            for (Menu m : menuData) {



                Integer role=-1;

                for (Map<String, Object> rmap : roleData) {

                    if (m.getMenuId() == rmap.get("menu_id") &&ur.getRoleId()==rmap.get("role_id")) {

                           //if(rmap.get("role").toString().equals(0)){



                          // }
                         role= Integer.parseInt(rmap.get("role").toString());

                         break;

                    }

                }

                pushMap.put(m.getMenuName(),role);



            }

            Map<String,Object> oneRoleMenuMap=new HashMap<String,Object>();

            oneRoleMenuMap.put(ur.getRoleName()+","+ur.getRoleId(),pushMap);

            roleMapData.add(oneRoleMenuMap);

        }

        pageVo.setResult(roleMapData);

        return pageVo;
    }

    @Override
    public List<Map<String,Object>> selectRoleMenuOne(Integer roleId) {
        return roleMenuDao.selectRoleMenuOne(roleId);
    }

    /**
     * vo对象赋值
     *
     * @param m
     * @param sgv
     * @return
     */
    private RoleMenu setRoleMenuValue(Menu m, SysGroupVo sgv) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(sgv.getRoleId());
        roleMenu.setMenuId(m.getMenuId());
        if (sgv.getGroupName().equals("用户管理")) {
            roleMenu.setRole(sgv.getCustomerManager());
        } else if (sgv.getGroupName().equals("律师管理")) {
            roleMenu.setRole(sgv.getCustomerManager());
        } else if (sgv.getGroupName().equals("咨询管理")) {
            roleMenu.setRole(sgv.getConsultManager());
        } else if (sgv.getGroupName().equals("渠道管理")) {
            roleMenu.setRole(sgv.getChannelManager());
        } else if (sgv.getGroupName().equals("财务结算管理")) {
            roleMenu.setRole(sgv.getFinanceManager());
        } else if (sgv.getGroupName().equals("运营规则设定")) {
            roleMenu.setRole(sgv.getOperateManager());
        } else if (sgv.getGroupName().equals("市场活动设定")) {
            roleMenu.setRole(sgv.getMarketManager());
        } else if (sgv.getGroupName().equals("场景充值管理")) {
            roleMenu.setRole(sgv.getScenceManager());
        } else if (sgv.getGroupName().equals("数据分析")) {
            roleMenu.setRole(sgv.getDataManager());
        } else if (sgv.getGroupName().equals("账号管理")) {
            roleMenu.setRole(sgv.getSysuserManager());
        }
        return roleMenu;
    }


    @Override
    public void updateRoleMenuOne(Integer roleId, List<RoleMenu> list) {


        roleMenuDao.deleteRoleMenuForRoleId(roleId);


        if(list.size()!=0){

            roleMenuDao.insertRoleMenuBatch(list);

        }


    }

    @Override
    public void addRoleMenuOne(String roleName, List<RoleMenu> list) {

        UserRole ur=new UserRole();

        ur.setRoleName(roleName);

        userRoleDao.insert(ur);

        if(list.size()!=0){

            for (RoleMenu r:list) {

                 r.setRoleId(ur.getRoleId());

            }

            roleMenuDao.insertRoleMenuBatch(list);

        }

    }

    @Override
    public List<Menu> findRoleMenu(String userName) {
        return menuDao.findRoleMenu(userName);
    }

    @Override
    public List<Map<String, Object>> selectUserRoleMenu(String userName) {
        return roleMenuDao.selectUserRoleMenu(userName);
    }
}

